<?php
/*\
 *	file: oh.names.php
 *	------------------
 *
 *
 *
\*/

function &oh_module_parse($query) {
	global $oh;
	
	$i = oh_query_parse($query);

	$m = new stdClass;
	
	$m->name 		= $i['module'];
	$m->token 		= $i['tokens'];
	$m->have 		= count($i['tokens']);
	$m->payload 	= $i['payload'];
	$m->class	 	= "oh_class_".$i['module'];
	$m->function 	= "oh_function_".$i['module'];	
	$m->interface	= $i['module'].'.interface.php';			
	$m->object 		= "oh_object_".$i['module'];

	return $m;
}

function oh_module_default(&$m) {
	global ${$m->object};
	if (!isset(${$m->object})) {
		$m->access = "created by default";
		${$m->object} = new stdClass();
		${$m->object}->info = new oh_info($m);
	} else {
		$m->access = "already created";	
	}
}

function oh_module_call(&$m) {
	global $oh;
	$oh['module'][] = $m->name;
	$f = $m->function;
	$r = $f($m);
	return $r;	
}

function oh_module_require(&$m) {
	$p = oh_module_pack();
	foreach($p as $pack) {
		$f = $pack.'/'.$m->name.'/'.$m->interface;
		if (file_exists($f)) {
			require_once($f);
			$m->file = $f;		
			return true;
		}	
	}		
	return false;
}





function oh_module_exists($m) {	
	$i = oh_module_interface_file($m);
	if (file_exists($i)) return true;	
	$p = oh_module_packs();
	foreach($p as $pack) {
		$i = oh_module_interface_file($m,$pack);
		if (file_exists($i)) return true;
	}	
	return false;
}




function oh_module_init($m) {
	global ${$m->object};
	${$m->object} = new $m->class();
	${$m->object}->info = new oh_info($m);
}


function oh_query_parse($query) {		
	$i = array();
	$i['query'] = $query;
	if (strpos($query,":")!==FALSE) {
		$i['command'] = substr($query,0,strpos($query,":"));
		$i['payload'] = substr($query,strpos($query,":")+1);
	} else {
		$i['command'] = $query;
		$i['payload'] = "";	
	}
	if (strpos($i['command'],".")===FALSE) {
		$i['command'] = str_replace("_",".",$i['command']);
	} else {
		# nothing
	}
	$i['tokens'] = explode(".",$i['command']);
	$i['module'] = strtolower($i['tokens'][0]);
	return $i;
}

function &oh_this($o=null) {
	global $oh;

	$d = debug_backtrace();			
	$f = basename($d[0]["file"]);		
	$m = false;
	
	oh_dump($f);
	
	if (!$m) {
		$m = oh_module_get_by_filename($f);
	}
	if (!$m) {
		$m = oh_module_last_in_stack();
	}
	if ($m) {
		global ${"oh_object_".$m};
		if ($o!==null) {
			${"oh_object_".$m} = $o;
		}
		return ${"oh_object_".$m};		
	} else {
		oh_message(EM200);
	}
}

function oh_module_get_by_filename($f) {
	$f = explode(".",$f);
	if ($f[1]=="interface") {
		return $f[0];
	}
	return null;
}

function oh_module_last_in_stack() {
	global $oh;
	return $oh['module'][count($oh['module'])-1];
}


function oh_underscore($name) {
	return str_replace("-","_",$name);
}

function oh_this_name() {

}


function oh_math_integer_divide($x, $y) {
	if ($x == 0) return 0;
	if ($y == 0) return FALSE;
	return ($x - ($x % $y)) / $y;
}

function oh_name_sanitize($textname) {
	$name = $textname;
	$name = trim(strtolower($name));
	$name = str_replace(" ","-",$name);
	return $name;
}

function oh_is_name($textname) {
	if (preg_match("/^[a-zA-Z-0-9]+$/i",$textname)) {
		return true;
	} else {
		return false;	
	}
}

function oh_module_interface_file($modulename,$pack="") {
	global $oh;
	
	if ($pack) {
		return $oh['path'].'/modules/'.$pack.'/'.$modulename.'/'.$modulename.'.interface.php';		
	} else {
		return $oh['path'].'/modules/'.$modulename.'/'.$modulename.'.interface.php';	
	}
}

function oh_framework_is_installed() {
	global $oh;
	return is_dir($oh['path']."/includes");
}

		
function oh_var_name($var) {
    foreach($GLOBALS as $var_name => $value) {
        if ($value === $var) {
            return $var_name;
        }
    }	
    return false;
}


function oh_properties($s) {
	$o = array();
	$s = preg_replace("/[ \t\r\n]+/","",$s);
	$s = explode(",",$s);		
	foreach($s as $i) {
		$i = explode(":",$i);
		$o[$i[0]] = $i[1];
	}
	return $o;
}


